import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;

import edu.jhu.tcj.purfuit.Die;
import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.Turn;
import edu.jhu.tcj.purfuit.Turn.TurnState;


public class DieUI extends JPanel implements PropertyChangeListener{

	private final Die die;
	
	private int size = 40;
	private int dotSize = 6;

	private final Game game;

	public DieUI(Die die, Game game) {
		this.die = die;
		this.game = game;
		this.game.addPropertyChangeListener(Turn.TURN_STATE_UPDATE, this);
	}

	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		Graphics2D g = (Graphics2D)graphics;
		g.setColor(Color.white);
		g.fillRect(0, 0, size, size);
		
		g.setColor(Color.black);
		int value = die.getValue();
		switch (value) {
			case 1:	
				g.fillOval(size/2-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				break;
			case 2:
				g.fillOval(size/3-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				break;
			case 3:
				g.fillOval(size/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/2-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				break;
			case 4:
				g.fillOval(size/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				break;
			case 5:
				g.fillOval(size/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/2-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				break;
			case 6:
				g.fillOval(size/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size/3-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size/2-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size/3-dotSize/2, dotSize, dotSize);
				g.fillOval(size*2/3-dotSize/2, size*2/3-dotSize/2, dotSize, dotSize);
				break;
		}
		g.drawRect(0, 0, size, size);
	}
	
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		Object newValue = evt.getNewValue();
		if (newValue instanceof TurnState) {
			TurnState turnState = (TurnState)newValue;
			switch (turnState) {
				case WaitingToRoll:
					break;
				case WaitingToChooseSpace:
					break;
				case WaitingToAnswerQuestion:
					break;
				case TurnOver:
					break;
			}
			repaint();
		}
	}
}
